<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('login_model');
    }

	public function home($page = 'login')
	{

        if ( ! file_exists('application/views/pages/'.$page.'.php'))
        {
            // Whoops, we don't have a page for that!
            show_404();
        }

        if ($this->session->userdata('logged_in'))
        {
            redirect('announcement/view', 'refresh');
        }
        else
        {

            $data['title'] = ucfirst($page);

            $this->load->helper('form');

            $this->load->view('template/login_header', $data);
            $this->load->view('pages/'.$page, $data);
            $this->load->view('template/footer');

        }

	}

    public function login_user($page = 'login')
    {

        if ($this->session->userdata('logged_in'))
        {
            redirect('manage_department/manage_dept', 'refresh');
        }
        else
        {
            $this->load->library('form_validation');
            $data['title'] = ucfirst($page);

            $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
            $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_user');

            if ($this->form_validation->run() === FALSE)
            {

                $this->load->view('template/login_header', $data);
                $this->load->view('pages/'.$page, $data);
                $this->load->view('template/footer');

            }

            else
            {
                //Redirect after successful login
                redirect('announcement/view', 'refresh');
            }
        }

    }

    function check_user ($password)
    {

        $username = $this->input->post('username');
        $result = $this->login_model->check_acc($username, $password);

        if($result)
        {
            $sess_array = array();
            foreach($result as $row)
            {
                $sess_array = array(
                    'user_id' => $row->user_id,
                    'type_no' => $row->type_no,
                    'coll_id' => $row->coll_id
                );
            }

            $this->session->set_userdata('logged_in', $sess_array);
            return TRUE;

        }
        else
        {
            $this->form_validation->set_message('check_user', "Invalid Username or Password");
            return false;
        }

    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */